<?php

namespace App\Modules\Admin\Entities\Water;

use App\Models\Model;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsFreeModel;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsPurifyModel;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsSealModel;
use App\Modules\Admin\Entities\Member\UsersModel;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Cache;

class WaterDeviceModel extends Model
{
    protected $table='water_device';
    protected $primaryKey='water_id';
    protected $is_delete=0;


    protected $cache_key  = 'water:device:job_cache';



    public function getApparatusByCommunityId($communityId){
        return $this->where(['community_id'=>$communityId,'is_delete'=>0])->get()->toArray();
    }

    public function getSelectListsById($ids){
        return $this->with(['community'])->whereIn('water_id',$ids)->orderBy($this->getKeyName(), 'ASC')
            ->get(['water_id','water_title','code','mineral_title','rich_title','community_id','address'])->toArray();
    }
    public function getSelectListsByIds($ids){
        return $this->whereIn('water_id',$ids)->orderBy($this->getKeyName(), 'ASC')
            ->get(['water_id','water_title','code','profit_date','manage_profit_date'])->toArray();
    }
    public function getSelectListsByWaterId($water_id){
        return $this->where(['water_id'=>$water_id])->first(['water_id','water_title','code','mineral_title','rich_title','community_id','address']);
    }

    /**
     * @return HasOne
     */
    public function community(): HasOne
    {
        return $this->hasOne(CommunityModel::class, 'id', 'community_id')->select(['id','title']);
    }

    /**
     * @return HasOne
     */
    public function user(): HasOne
    {
        return $this->hasOne(UsersModel::class, 'id', 'buy_user_id')->select(['id','username','nickname','mobile']);
    }
    /**
     * 物业费 添加
     * @return HasOne
     */
    public function propertyFreeInfo(): HasOne
    {
        return $this->hasOne(ApparatusPropertyFreeModel::class, 'water_id');
    }
    /**
     * 物业费 log
     * @return HasOne
     */
    public function propertyFreeLogInfo(): HasOne
    {
        return $this->hasOne(ApparatusPropertyFeeLogModel::class, 'water_id');
    }
    /**
     * 电费
     * @return HasOne
     */
    public function  electricityInfo(): HasOne
    {
        return $this->hasOne(ApparatusElectricityModel::class, 'water_id');
    }
    /**
     *  电费 log
     * @return HasOne
     */
    public function electricityLogInfo(): HasOne
    {
        return $this->hasOne(ApparatusElectricityLogModel::class, 'water_id');
    }
    /**
     * 电费
     * @return HasOne
     */
    public function waterRateInfo(): HasOne
    {
        return $this->hasOne(ApparatusWaterRateModel::class, 'water_id');
    }
    /**
     * 设备参数     *
     * @return hasMany
     */
    public function params_free(): hasMany
    {
        return $this->hasMany(EquipmentParamsFreeModel::class, 'water_id');
    }
    /**
     * 设备参数     *
     * @return hasMany
     */
    public function params_purify(): hasMany
    {
        return $this->hasMany(EquipmentParamsPurifyModel::class, 'water_id');
    }
    /**
     * 设备参数     *
     * @return hasMany
     */
    public function params_seal(): hasMany
    {
        return $this->hasMany(EquipmentParamsSealModel::class, 'water_id');
    }
    public function getWateInfoById($key)
    {

    }


    /**
     * 删除缓存
     *
     * @return bool
     */
    public function delCache()
    {
        Cache::forget($this->cache_key);
        return true;
    }
    /**
     * 获取商品分类的数据缓存
     *
     * @param  string  $get_type
     *
     * @return mixed
     */
    public function getCache(string $get_type = '')
    {
            $all = $this->where(['status'=>1])->get()->toArray();
            $tree_cache=[];
            $equipment_tree=[];
            foreach ($all as $item){
                $equipment_id = $item['equipment_id'];
                $equipment_id_rich = $item['equipment_id_rich'];
                $equipment_tree[]=$equipment_id;
                $equipment_tree[]=$equipment_id_rich;
                $tree_cache[$equipment_id]=['water_id'=>$item['water_id'],'water_title'=>$item['water_title'],'type'=>[1,2]];
                $tree_cache[$equipment_id_rich]=['water_id'=>$item['water_id'],'water_title'=>$item['water_title'],'type'=>3];
                Cache::set($this->cache_key.':equipment',$equipment_tree);
                Cache::set($this->cache_key.":eq_all:".$equipment_id,['water_id'=>$item['water_id'],'water_title'=>$item['water_title'],'type'=>[1,2]]);
                Cache::set($this->cache_key.":eq_all:".$equipment_id_rich,['water_id'=>$item['water_id'],'water_title'=>$item['water_title'],'type'=>[3]]);
            }
            return $equipment_tree;
    }
    /**
     * 获取Tree格式化数据
     *
     * @return array|mixed
     */
    public function getCacheTree()
    {
        return $this->getCache()['tree'] ?? [];
    }


}
